package com.mc.cloud.mall.common.common;

import com.mc.cloud.mall.common.exception.MallExceptionEnum;

/**
 * @ClassName MallRestResponse
 * @Author MC
 * @Date 2021/5/20 12:50
 * @Version 1.0
 **/
public class MallRestResponse<T> {
    
    private Integer status;
    
    private String msg;
    
    private T data;
    
    private static final int OK_CODE = 10000;
    
    private static final String OK_MSG = "SUCCESS";
    
    public MallRestResponse(Integer status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }
    
    public MallRestResponse(Integer status, String msg) {
        this.status = status;
        this.msg = msg;
    }
    
    public MallRestResponse() {
        this(OK_CODE,OK_MSG);
    }
    
    //成功
    public static <T> MallRestResponse<T> success(){
        return new MallRestResponse<>();
    }
    
    
    //请求成功，并且带上返回值
    public static <T> MallRestResponse<T> success(T result){
        MallRestResponse<T> response = new MallRestResponse<>();
        response.setData(result);
        return response;
    }
    
    //失败
    public static <T> MallRestResponse<T> error(Integer code,String msg){
        return new MallRestResponse<>(code,msg);
    }
    
    //失败 --枚举
    public static <T> MallRestResponse<T> error(MallExceptionEnum ex){
        return new MallRestResponse<>(ex.getCode(),ex.getMsg());
    }
    
    @Override
    public String toString() {
        return "MallRestResponse{" +
                "status=" + status +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
    
    public Integer getStatus() {
        return status;
    }
    
    public void setStatus(Integer status) {
        this.status = status;
    }
    
    public String getMsg() {
        return msg;
    }
    
    public void setMsg(String msg) {
        this.msg = msg;
    }
    
    public T getData() {
        return data;
    }
    
    public void setData(T data) {
        this.data = data;
    }
    
    public static int getOkCode() {
        return OK_CODE;
    }
    
    public static String getOkMsg() {
        return OK_MSG;
    }
}
